<?php

/**
 * Current locale choise and translator initialization
 *
 * @author user4
 */
class Bluembo_Localization {

    /**
     * configuration data, translator adapter type
     * @var string
     */
    private $_adapter = null;

    /**
     * configuration data, translator resourses files path, use {lng} mask for selected language replacement
     * @var string
     */
    private $_resourcesPath = null;

    /**
     * configuration data, default language name
     * @var string
     */
    private $_defaultLanguage = null;

    /**
     * configuration data, allowed locales list
     * @var array of string, language => locale
     */
    private $_allowedLocales = null;

    /**
     * incoming data, received request
     * @var Zend_Controller_Request
     */
    private $_request = null;

    /**
     * incoming data, session storage
     * @var Zend_Session
     */
    private $_session = null;

    /**
     * current language name
     * @var string
     */
    private $_language = null;

    /**
     * current locale
     * @var Zend_Locale
     */
    private $_locale = null;

    public function __construct(Zend_Controller_Request_Abstract $request)
    {
        $config = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('config');
        $this->_adapter = $config->localization->adapter;

        $this->_resourcesPath = $config->localization->path;
        $this->_defaultLanguage = $config->localization->default;
        $this->_allowedLocales = $config->localization->allowed->toArray();

        $this->_request = $request;
        $this->_session = new Zend_Session_Namespace('localization');

        $this->_language = '';
        $this->_locale = new Zend_Locale(Zend_Locale::BROWSER);
    }

    public function initLocale()
    {
        $this->_setLanguageByRequest();
        $this->_setLanguageBySession();
        $this->_setLanguageByBrowser();

        $this->_checkSelectedLanguage();

        $this->_storeLocale();
        $this->_initTranslator();
    }

    private function _initTranslator()
    {
        $path      = realpath(str_replace('{lng}', $this->_language, $this->_resourcesPath));
        $translate = new Zend_Translate(
                        array(
                            'adapter' => $this->_adapter,
                            'content' => $path,
                            'locale' => $this->_language
                        )
        );
        Zend_Registry::set('Zend_Translate', $translate);
    }

    private function _setLanguageByRequest()
    {
        $this->_language = $this->_request->getParam('language', '');
    }

    private function _setLanguageBySession()
    {
        if (empty($this->_language) && !empty($this->_session->language)) {
            $this->_language = $this->_session->language;
        }
    }

    private function _setLanguageByBrowser()
    {
        $locale = $this->_locale->toString();
        if (empty($this->_language) && !empty($this->_locale)) {
            $this->_language = $locale;
        }
    }

    private function _checkSelectedLanguage()
    {
        if (!isset($this->_allowedLocales[$this->_language])) {
            $this->_language = array_search($this->_language, $this->_allowedLocales);  // some browser send locale instead of language
            if (!$this->_language) $this->_language = $this->_defaultLanguage;
        }
    }

    private function _storeLocale()
    {
        $this->_session->language = $this->_language;
        $this->_locale->setLocale($this->_allowedLocales[$this->_language]);
        Zend_Registry::set('Zend_Locale', $this->_locale);
    }

}